package com.mnt.gui.base.util;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXMLLoader;

import com.mnt.gui.base.controller.BaseController;

/**
 * FXML loader tool<br/>
 * controller load<br/>
 * fxml file load<br/>
 * i18n file load<br/>
 * 
 * @author mnt.cico
 *
 */
public class FXMLLoaderTool<C extends BaseController> {

	private FXMLLoaderTool(){
		//empty
	}
	
	private static final String RES_DEFALUR_PATH = "res/";
	
	/**
	 * init the class conver to BaseController object
	 * @author mnt.cico
	 * @param c
	 * @return
	 */
	public static final <C extends BaseController> C load(C c, URL fxmlFileUrl,  ResourceBundle i18nBUNDLE){
		C controller = c;
		FXMLLoader loader = new FXMLLoader(fxmlFileUrl);
		loader.setController(controller);
		loader.setRoot(controller);
		try {
			loader.load();
			c.init();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return controller;
	}
	
	/**
	 * builder class to controller
	 * 2014-3-29 mnt.cico
	 */
	public static final <C extends BaseController>C load(Class<C> c){
		C controller = null;
		try {
			controller = c.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return load(controller, controller.getClass().getResource(RES_DEFALUR_PATH + controller.getClass().getSimpleName() + ".fxml"), null);
	}
	
	/**
	 * load fxml file to object 
	 * 2014-6-30 mnt.cico
	 * @param baseController
	 * @return
	 */
	public static final <C extends BaseController>C load(C baseController) {
		return load(baseController, baseController.getClass().getResource(RES_DEFALUR_PATH + baseController.getClass().getSimpleName() + ".fxml"), null);
	}
	
}
